package com.beamfield.controller.api.mb;

import com.alibaba.fastjson.JSONObject;
import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.LogInfo;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.MbBaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.DeviceInspect;
import com.beamfield.model.DeviceMaintain;
import com.beamfield.modelVo.Qrcode;
import com.beamfield.service.manage.device.DeviceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Zephyr
 * @Description: 移动端——设备接口
 * @Date 2018/10/10
 */
@RequestMapping(value = "/mb_api/device")
@Controller
public class MbDeviceController extends MbBaseController {

    private final DeviceService deviceService;

    @Autowired
    public MbDeviceController(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    /**
     * 根据二维码信息获取设备信息
     * @return
     */
    @RequestMapping(value = "/getDeviceInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getDeviceInfo(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String qrCodeJson = body.getString("qrCodeJson");
        Qrcode qrcode = super.getQrcode(qrCodeJson);
        if(qrcode == null){
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), "无法解析二维码信息");
        }
        LogInfo logInfo = super.getLogInfo(request);
        if(!Objects.equals(qrcode.getBeam(), String.valueOf(logInfo.getBeamId()))){
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), "此二维码不属于当前登录梁场");
        }
        try{
            return new ApiResult(deviceService.getDeviceInfoByInboundId(Long.parseLong(qrcode.getData())));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取进场id设备信息
     * @return
     */
    @RequestMapping(value = "/getDeviceInfoById", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getDeviceInfoById(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long inboundId = body.getLong("inboundId");
        if(inboundId == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return new ApiResult(deviceService.getDeviceInfoByInboundId(inboundId));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取保养维护记录
     * @param request
     * @return
     */
    @RequestMapping(value = "/getMaintainDetail", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getMaintainDetail(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long inboundId = body.getLong("inboundId");
        Long deviceId = body.getLong("deviceId");
        Long offset = body.getLong("offset");
        Integer limit = body.getInteger("limit");
        if(inboundId == null || deviceId == null || offset == null || limit == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        LogInfo logInfo = super.getLogInfo(request);
        try {
            List<DeviceMaintain> maintainById = deviceService.getMaintainByInboundId(inboundId, deviceId, offset,limit,null,null, logInfo.getBeamId());
            long maintainNums = deviceService.getMaintainNums(inboundId, deviceId, null, null);
            PageResult pageResult = new PageResult(maintainNums, offset, maintainById);
            return new ApiResult(pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }
    /**
     * 获取日常检查记录
     * @param request
     * @return
     */
    @RequestMapping(value = "/getInspect", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getInspect(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long inboundId = body.getLong("inboundId");
        Long deviceId = body.getLong("deviceId");
        String offset = body.getString("offset");
        String limit = body.getString("limit");
        if(inboundId == null || deviceId == null || StringUtils.isEmpty(offset) || StringUtils.isEmpty(limit)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try {
            List<DeviceInspect> workById = deviceService.getInspectById(inboundId, deviceId, offset, limit);
            long total = deviceService.countInspectById(inboundId, deviceId);
            PageResult pageResult = new PageResult(total, CommonUtils.parseLong(offset, 0L), workById);
            return new ApiResult(pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 保存日常检查记录
     * @param request
     * @return
     */
    @RequestMapping(value = "/saveInspect", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveInspect(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        LogInfo logInfo = super.getLogInfo(request);
        try {
            DeviceInspect deviceInspect = body.toJavaObject(DeviceInspect.class);
            if(deviceInspect == null){
                return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
            }
            return deviceService.saveInspect(deviceInspect, logInfo.getBeamId(), logInfo.getUserId());
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取首页统计数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/getStatisticsData", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getStatisticsData(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        LogInfo logInfo = super.getLogInfo(request);
        String query = body.getString("query");

        try{
            return deviceService.getMbStatisticsData(query, logInfo.getBeamId());
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }
    /**
     * 搜索设备
     * @return
     */
    @RequestMapping(value = "/getDeviceList", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getDeviceList(HttpServletRequest request){
        Long beamId = super.getLogInfo(request).getBeamId();

        JSONObject jsonObject = super.getBody(request);
        if(jsonObject == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Integer offset = jsonObject.getInteger("offset");
        Integer limit = jsonObject.getInteger("limit");
        String query = jsonObject.getString("query");
        if(StringUtils.isEmpty(query)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return new ApiResult(deviceService.getMbDeviceList(offset,limit,query,beamId));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取试运行记录
     */
    @RequestMapping(value = "/getDebug", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getDebug(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String inboundId = body.getString("inboundId");
        if(StringUtils.isEmpty(inboundId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try {
            return deviceService.getInBoundById(Long.parseLong(inboundId));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode());
        }
    }
}
